package controller

import (
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"user_bff/model"
	"user_bff/pkg"
	"user_bff/user_proto"
)

func BookList(c *gin.Context) {
	name := c.Query("name")
	page := c.Query("page")
	limit := c.Query("limit")
	pageInt, _ := strconv.Atoi(page)
	limitInt, _ := strconv.Atoi(limit)
	if pageInt == 0 {
		pageInt = 1
	}
	if limitInt == 0 {
		limitInt = 3
	}
	if name == "" {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "参数不能为空",
			"data": nil,
		})
		return
	}
	comment := pkg.Comment()
	res, err := comment.BookList(context.Background(), &user_proto.BookListRequest{
		Name:  name,
		Page:  int64(pageInt),
		Limit: int64(limitInt),
	})
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "查询漫画列表失败",
			"data": nil,
		})
		return
	}
	model.SetBookList(res.BookInfo)

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "查询成功",
		"data": res,
	})

}
func BookLists(c *gin.Context) {
	types := c.Query("types")
	page := c.Query("page")
	limit := c.Query("limit")
	pageInt, _ := strconv.Atoi(page)
	limitInt, _ := strconv.Atoi(limit)
	if pageInt == 0 {
		pageInt = 1
	}
	if limitInt == 0 {
		limitInt = 3
	}
	if types == "" {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "参数不能为空",
			"data": nil,
		})
		return
	}
	comment := pkg.Comment()
	res, err := comment.BookLists(context.Background(), &user_proto.BookListsRequest{
		Types: types,
		Page:  int64(pageInt),
		Limit: int64(limitInt),
	})
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "查询漫画列表失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "查询成功",
		"data": res,
	})

}
